/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package z.h.w.jar.kit.code.pic;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Point;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import z.h.w.jar.kit.method.Foto;
import z.h.w.jar.kit.method.IO;
import z.h.w.jar.kit.method.Roll;
import z.h.w.jar.kit.method.To;

/**
 *
 * @author EnzoZhong
 */
public class Charaecter2Image implements Code {

      private BufferedImage image;
      private String charaecter;
      private Graphics2D g;
      private int picSize;
      private Boolean isDrawString = false;

      /**
       *
       * @param cha
       * @param color
       * @param font
       * @param fontSize
       */
      public Charaecter2Image ( Character cha , Color color , Font font , Integer fontSize ) {
            this.charaecter = cha.toString ();
            this.picSize = To.num2Integer ( fontSize * 4.0 );
            image = new BufferedImage ( picSize , picSize , BufferedImage.TYPE_INT_RGB );
            image = image.createGraphics ().getDeviceConfiguration ().createCompatibleImage ( picSize , picSize , Transparency.TRANSLUCENT );
            g = image.createGraphics ();

            g.setColor ( color );
            font = font.deriveFont ( Float.valueOf ( fontSize ) );
            g.setFont ( font );
            if ( charaecter.equalsIgnoreCase ( " " ) ) {
                  image.setRGB ( 0 , 0 , color.getRGB () );
                  image.setRGB ( fontSize , fontSize , color.getRGB () );
            } else {
                  g.drawString ( charaecter , To.num2Integer ( picSize * 0.28 ) , To.num2Integer ( picSize * 0.65 ) );
            }
            isDrawString = true;
      }

      /**
       *
       */
      public void drawBorder () {
            Foto.Border minMax = Foto.findBorder ( image );
            Point min = minMax.min;
            Point max = minMax.max;
            g.drawRect ( min.x , min.y , max.x - min.x + 1 , max.y - min.y + 1 );
      }

      /**
       *
       * @param degree
       */
      public void rotate ( Integer degree ) {
            if ( isDrawString ) {
                  //求出之前图片的斜边长，也就是园的直径
                  Integer intDiametro = Foto.getDiametro ( image ).intValue ();
                  //以该直径长，生成一个正方形
                  BufferedImage rotatePlace =
                                new BufferedImage ( intDiametro , intDiametro , BufferedImage.TYPE_INT_RGB );
                  Graphics2D tmpG2D = rotatePlace.createGraphics ();
                  GraphicsConfiguration gc = tmpG2D.getDeviceConfiguration ();
                  rotatePlace = gc.createCompatibleImage ( intDiametro , intDiametro , Transparency.TRANSLUCENT );
                  Graphics2D g2d = rotatePlace.createGraphics ();
                  //旋转画笔，把图片写入
                  g2d.rotate ( degree * Math.PI / 180 , intDiametro / 2 , intDiametro / 2 );
                  //画在新图层的中心上
                  Integer x = ( intDiametro - image.getWidth () ) / 2;
                  Integer y = ( intDiametro - image.getHeight () ) / 2;
                  g2d.drawImage ( image , x , y , null );
                  g2d.dispose ();
                  image = rotatePlace;
            } else {
                  g.rotate ( degree * Math.PI / 180 , picSize / 2 , picSize / 2 );
            }

      }

      /**
       *
       * @return
       */
      @Override
      public BufferedImage getImage () {
            g.dispose ();
            Foto.Border minMax = Foto.findBorder ( image );
            Point min = minMax.min;
            Point max = minMax.max;
            image.setRGB ( min.x , min.y , 0 );
            image.setRGB ( max.x , max.y , 0 );
            return image.getSubimage ( min.x , min.y , max.x - min.x + 1 , max.y - min.y + 1 );
      }

      /**
       *
       * @param args
       * @throws FontFormatException
       * @throws IOException
       */
      public static void main ( String[] args ) throws FontFormatException , IOException {

            File[] files = new File ( "c:\\" ).listFiles ();
            for ( File file : files ) {
                  if ( file.toString ().contains ( ".png" ) ) {
                        file.delete ();
                  }
            }

            File file = new File ( "D:\\App\\lib\\wqy-zenhei.ttc" );
            Font font = Font.createFont ( 0 , file );

            Character[] list = To.string2CharacterArray ( "cosa sta faccendo?" );
            int base = 50;
            for ( Character str : list ) {
                  Charaecter2Image pic = new Charaecter2Image ( str , Roll.rollColor () , font , base );
                  IO.bufferedImage2Png ( pic.getImage () , "c:\\" + System.currentTimeMillis () + ".png" );
            }
      }

      /**
       *
       * @return
       */
      @Override
      public String getString () {
            return charaecter;
      }

}
